<?php
	/**
	 *	Wap Portal Server
	 *
	 *	Copyright (C) 2002 - 2004 Emir Sakic
	 *
	 *	Wap Portal Server
	 *	Developer: Emir Sakic - saka@hotmail.com
	 *  Homepage: http://www.sakic.net
	 *	Date: 15/03/2004
	 * 	Version #: 1.2
	**/

if (empty($action)) {
	$action = "";
} else {
	$action = strtolower($action);
}

// Init
if (empty($id)) {
	$id = null;
}
if (empty($sectionid)) {
	$sectionid = null;
}
if (empty($published)) {
	$published=0;
}

switch($action) {
	case "new":
		display_edit_form($id, $sectionid);
		break;
	case "edit":
		display_edit_form($id, $sectionid);
		break;
	case "save":
		save($id, $sectionid, $title, $content, $published);
		break;
	case "delete":
		delete($id);
		break;
	case "publish":
		publish($id);
		break;
	case "reorder":
		reorder ($id, $direction);
		break;
	default:
		display_items($sectionid);
		break;
}

function publish ($id) {
	global $sectionid;
	$query = "SELECT published FROM wps_items WHERE id='$id'";
	$result = mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	$row = mysql_fetch_object($result);

	if ($row->published) {
		$query = "UPDATE wps_items SET published=0 WHERE id = '$id'";
		mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	} else {
		$query = "UPDATE wps_items SET published=1 WHERE id = '$id'";
		mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	}
	display_items($sectionid);
}

function reorder ($id, $direction) {
	global $sectionid;

	$query = "SELECT sectionid, ordering FROM wps_items WHERE id = '$id'";
	$result = mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	$row = mysql_fetch_object($result);
	$ordering = $row->ordering;

	$query = "SELECT id FROM wps_items WHERE sectionid='$row->sectionid'";
	$result = mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	$total = mysql_num_rows($result);

	if ($direction=="up" && $ordering>0) {
		$ordering--;
		$query = "UPDATE wps_items SET ordering='$ordering' WHERE id = '$id'";
		mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	} else if ($direction=="down" && $ordering<$total) {
		$ordering++;
		$query = "UPDATE wps_items SET ordering='$ordering' WHERE id = '$id'";
		mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	}
	display_items($sectionid);
}

function delete ($id) {
	global $sectionid;
	if (is_array($id)) {
		foreach($id as $value) {
			$query = "DELETE FROM wps_items WHERE id = '$value'";
			mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
		}
	} else {
		$query = "DELETE FROM wps_items WHERE id = '$id'";
		mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	}
	display_items($sectionid);
}

function save($id, $sectionid, $title, $content, $published) {
	// Replace quotes
	if (!get_magic_quotes_gpc()) {
		$title = str_replace("'","\'",$title);
		$content = str_replace("'","\'",$content);
	}
	$date = date ("Y-m-d H:i:s");
	if ($id) {
		$query = "UPDATE wps_items SET sectionid='$sectionid', title = '$title', content = '$content', date = '$date', published='$published' WHERE id = '$id'";
	} else {
		$query = "INSERT INTO wps_items VALUES ('', '$sectionid', '$title', '$content', '$date', 0, '$published')";
	}
	mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());

	if ($id) {
		display_edit_form($id, $sectionid);
	} else {
		display_items($sectionid);
	}
}

function display_edit_form($id, $sectionid) {
	global $common;

	$query = "SELECT * FROM wps_timediff";
	$result = mysql_query($query);
	$row = mysql_fetch_row ($result);
	$time_offset = $row[1] * 60 * 60;

	if (isset($id)) {
		if (is_array($id)) $id = $id[0];
		$query = "SELECT * FROM wps_items WHERE id = '$id'";
		$result = mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
		$row = mysql_fetch_object($result);
	}
	echo "<table width=\"600\">\n";
    echo "<tr>\n";
    echo "<td><a href=\"index.php\" border=0>Home</a> &gt;
    	<a href=\"index.php?option=items\" border=0>Item Management</a> &gt;
    	Add / Edit</td>\n";
    echo "</tr>\n";
    echo "<tr>\n";
    echo "<td align=\"center\"><b>Item Management</b></td>\n";
    echo "</tr>\n";
    echo "<tr>\n";
    echo "<td valign=\"top\">\n";
    echo "<br>\n";
	echo "<form name=\"form\" method=\"post\" action=\"index.php?option=items\" onsubmit=\"return validate()\">\n";
	$query2 = "SELECT * FROM wps_sections ORDER BY title";
	$result2 = mysql_query ($query2);
	echo "<b>Section:</b>&nbsp;\n";
	echo "<br>\n";
	echo "<select name=\"sectionid\">\n";
	for ($i = 1; $row2 = mysql_fetch_object ($result2); $i++) {
		echo "\t\t\t<option value=\"$row2->id\"";
		if ($row2->id == $row->sectionid) {
			echo " selected";
		}
		echo ">$row2->title</option>\n";
	}
	echo "</select>\n";
	echo "<br>\n";
	echo "<b>Title:</b>\n";
	echo "<br>\n";
    echo "<input type=\"text\" name=\"title\" size=\"40\" value=\"";
    if (isset($id)) echo $row->title;
    echo "\">\n";
    echo "<br>\n";
    if (isset($id)) {
		$arrDateTime = explode(" ", $row->date);
		$arrDate = explode("-", $arrDateTime[0]);
		$arrTime = explode(":", $arrDateTime[1]);
		echo date ("Y-m-d H:i:s", mktime ($arrTime[0],$arrTime[1],$arrTime[2],$arrDate[1],$arrDate[2],$arrDate[0]) + $time_offset);
    } else {
		echo date ("Y-m-d H:i:s");
    }
	echo "<br>\n";
	echo "<b>Content:</b>\n";
	echo "<br>\n";
	echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
	echo "<tr>\n";
	echo "<td>\n";
    echo "<textarea name=\"content\" rows=\"12\" cols=\"60\" onselect=\"storeCaret(this);\" onclick=\"storeCaret(this);\" onkeyup=\"storeCaret(this);\">";
    if (isset($id)) echo $row->content;
	echo "</textarea>\n";
	echo "</td>\n";
	echo "<td width=\"50%\">\n";
	insertUBBbuttons();
	echo "</td>\n";
	echo "</tr>\n";
	echo "</table>\n";
	insertUBBhelp();
	echo "<br>\n";
	echo "Published: <input type=\"checkbox\" name=\"published\" value=\"1\"";
	if (empty($id) || $row->published==1) echo " checked";
	echo ">\n";
    echo "<br>\n";
    echo "<br>\n";
    echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
    echo "<tr>\n";
    echo "<td>\n";
    if (isset($id))
    	echo "<input type=\"hidden\" name=\"id\" value=\"$id\">\n";
    echo "<input type=\"hidden\" name=\"action\" value=\"save\">\n";
    echo "<input type=\"submit\" name=\"Submit\" value=\"Submit\">\n";
    echo "<input type=\"button\" name=\"Cancel\" value=\"Cancel\" onclick=\"document.location.href='index.php?option=items";
    if (isset($sectionid)) echo "&sectionid=$sectionid";
    echo "'\">\n";
    echo "</td>\n";
	echo "<td align=\"right\">\n";
	$common->setup_default_emulator();
	$common->display_emulator();
	echo "</td>\n";
    echo "</tr>\n";
    echo "</table>\n";
    echo "</form>\n";
    echo "</td>\n";
    echo "</tr>\n";
	echo "</table>\n";
}

function display_items($sectionid) {
	global $limit, $limitstart, $common;

	$query = "SELECT * FROM wps_timediff";
	$result = mysql_query($query);
	$row = mysql_fetch_row ($result);
	$time_offset = $row[1] * 60 * 60;

	echo "<table width=\"600\">\n";
    echo "<tr>\n";
    echo "<td><a href=\"index.php\" border=0>Home</a> &gt; Item Management</td>\n";
    echo "</tr>\n";
    echo "<tr>\n";
    echo "<td align=\"center\"><b>Items</b></td>\n";
    echo "</tr>\n";
    echo "<tr>\n";
    echo "<td align=\"right\" valign=\"top\">\n";

	$query = "SELECT * FROM wps_sections ORDER BY title";
	$result = mysql_query ($query);

	echo "<br>\n";
	echo "<b>Section:</b>&nbsp;\n";
	echo "<select onchange=\"document.location.href='index.php?option=items&sectionid=' + this.options[selectedIndex].value\">\n";
	echo "<option value=\"\">All</option>\n";
	for ($i = 1; $row = mysql_fetch_object ($result); $i++) {
		echo "\t\t\t<option value=\"$row->id\"";
		if ($sectionid == $row->id) {
			echo " selected";
		}
		echo ">$row->title</option>\n";
	}
	echo "</select>\n";

	// Select query
	if (empty($limitstart)) $limitstart = 0;
	if (empty($limit)) $limit = 10;

	if ($sectionid) {
		// Total
		$query = "SELECT id FROM wps_items WHERE sectionid = '$sectionid'";
		$result = mysql_query($query) or die("Query failed");
		$total = mysql_num_rows ($result) or die ("Query failed");
		if ($limit > $total) {
			$limitstart = 0;
		}
		$query = "SELECT * FROM wps_items WHERE sectionid = '$sectionid' ORDER BY sectionid, ordering LIMIT $limitstart, $limit";
	} else {
		// Total
		$query = "SELECT id FROM wps_items WHERE sectionid <> 0";
		$result = mysql_query($query) or die("Query failed");
		$total = mysql_num_rows ($result) or die ("Query failed");
		if ($limit > $total) {
			$limitstart = 0;
		}
		$query = "SELECT * FROM wps_items WHERE sectionid <> 0 ORDER BY sectionid, ordering LIMIT $limitstart, $limit";
	}
	$result = mysql_query ($query);

    echo "<br>\n";
	echo "<form name=\"form\" method=\"post\" action=\"index.php?option=items\">\n";
	echo "<table cellpadding=\"2\" cellspacing=\"0\" border=\"0\" width=\"100%\">\n";
    echo "<tr>\n";
	echo "<td width=\"5%\"><input type=\"checkbox\" name=\"toggle\" value=\"\" onclick=\"checkAll();\"></td>\n";
	echo "<td width=\"35%\"><b>Title</b></td>\n";
	echo "<td width=\"30%\"><b>Date</b></td>\n";
	echo "<td width=\"10%\" align=\"center\"><b>Reorder</b></td>\n";
	echo "<td width=\"10%\" align=\"center\"><b>Published</b></td>\n";
    echo "</tr>\n";
    echo "<tr><td colspan=\"5\">&nbsp;</td></tr>\n";
	for($i = 1; $row = mysql_fetch_object ($result); $i++) {

		$arrDateTime = explode(" ", $row->date);
		$arrDate = explode("-", $arrDateTime[0]);
		$arrTime = explode(":", $arrDateTime[1]);
		$date = date ("Y-m-d H:i:s", mktime ($arrTime[0],$arrTime[1],$arrTime[2],$arrDate[1],$arrDate[2],$arrDate[0]) + $time_offset);
	    echo "<tr>\n";
		echo "<td><input name=\"id[]\" type=\"checkbox\" value=\"$row->id\"></td>\n";
		echo "<td><a href=\"index.php?option=items&action=edit&id=$row->id&sectionid=$sectionid\">$row->title</a></td>\n";
		echo "<td>$date</td>\n";
		echo "<td align=\"center\"><a href=\"index.php?option=items&action=reorder&sectionid=$sectionid&id=$row->id&direction=up\"><img src=\"images/arrow_up.png\" alt=\"Up\" width=\"12\" height=\"12\" border=\"0\"></a>
			$row->ordering
			<a href=\"index.php?option=items&action=reorder&sectionid=$sectionid&id=$row->id&direction=down\"><img src=\"images/arrow_down.png\" alt=\"Down\" width=\"12\" height=\"12\" border=\"0\"></a></td>\n";
		if ($row->published) {
			echo "<td align=\"center\"><a href=\"index.php?option=items&action=publish&sectionid=$sectionid&id=$row->id\"><img src=\"images/publish_tick.png\" alt=\"Unpublish\" width=\"12\" height=\"12\" border=\"0\"></a></td>\n";
		} else {
			echo "<td align=\"center\"><a href=\"index.php?option=items&action=publish&sectionid=$sectionid&id=$row->id\"><img src=\"images/publish_x.png\" alt=\"Publish\" width=\"12\" height=\"12\" border=\"0\"></a></td>\n";
		}
		echo "</tr>\n";
	}
	echo "<tr><td colspan=\"5\">&nbsp;</td></tr>\n";
	echo "<tr><td colspan=\"5\" align=\"center\">";
	writePagesLinks($limitstart, $limit, $total, "items&sectionid=$sectionid");
	echo "</td></tr>\n";
	echo "<tr><td colspan=\"5\" align=\"center\">";
	writePagesCounter($limitstart, $limit, $total);
	echo "</td></tr>\n";
    echo "<tr>\n";
    echo "<td colspan=\"3\">
    	<input type=\"hidden\" name=\"sectionid\" value=\"$sectionid\">
		<input type=\"submit\" name=\"action\" value=\"New\">
		<input type=\"submit\" name=\"action\" value=\"Edit\">
		<input type=\"submit\" name=\"action\" value=\"Delete\" onclick=\"return confirm_delete();\">\n";
	echo "</td>\n";
	echo "<td colspan=\"2\" align=\"right\">\n";
	$common->setup_default_emulator();
	$common->display_emulator();
	echo "</td>\n";
	echo "</tr>\n";
	echo "</table>\n";
	echo "</form>\n";
    echo "</td>\n";
    echo "</tr>\n";
	echo "</table>\n";
}
?>